﻿using System;
using System.Collections.Generic;
using System.Text;

using NHibernate;
using Zwms.Data.BusinessObjects;
using Zwms.Data.Base;
using Zwms.Data.CommonObject;
using NHibernate.Criterion;
using System.Reflection;

namespace Zwms.Data.ManagerObjects
{
    public partial interface IStatStockViewManager : IManagerBase<Zwms.Data.BusinessObjects.StatStockView, string>
    {
        // Get Methods
        IList<StatStockView> GetLikeList(StatStockView record, ref PagerIndex pagerIndex);
    }

    partial class StatStockViewManager : ManagerBase<Zwms.Data.BusinessObjects.StatStockView, string>, IStatStockViewManager
    {
         #region Constructors

        public StatStockViewManager()
            : base()
        {
        }
        public StatStockViewManager(INHibernateSession session)
            : base(session)
        {
        }

        #endregion

        #region Get Methods
        public IList<StatStockView> GetLikeList(StatStockView record, ref PagerIndex pagerIndex)
        {
            ICriteria criteria = Session.GetISession().CreateCriteria(typeof(StatStockView));
            foreach (PropertyInfo pi in record.GetType().GetProperties())
            {
                if (pi.PropertyType.Name == "String")
                {
                    String temValue = (String)pi.GetValue(record, null);
                    if (!string.IsNullOrWhiteSpace(temValue))
                    {
                        criteria.Add(NHibernate.Criterion.Expression.Like(pi.Name, "%"+temValue+"%"));
                    }
                }
            }

            criteria.List<StatStockView>();
            //获取记录总数
            ICriteria pagecriteria = CriteriaTransformer.Clone(criteria);
            pagerIndex.RecordCount = Convert.ToInt32(pagecriteria.SetProjection(Projections.RowCount()).UniqueResult());
            //criteria.SetProjection();
            //设置排序
            //criteria.AddOrder(new Order("EquipmentID", false));
            //设置分页          
            criteria.SetFirstResult((pagerIndex.CurrentPageIndex - 1) * pagerIndex.PageSize)
                    .SetMaxResults(pagerIndex.PageSize);
            return criteria.List<StatStockView>();
        }
        #endregion
    }
}
